<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Adminlog Controller
 * Build by Phuc Nguyen
 * Contact : nguyenvanphuc0626@gmail.com
 */

class BookStatistic_model extends MY_Model{
	
	public function __construct(){
		parent::__construct();
		$this->table_name = 'book_statistic';
	}
        
        public function updateStatisticBookHold($params){
                $sql = 'UPDATE '.$this->table_name.' SET `booking_hold` = `booking_hold` + 1 WHERE `owner` = "'.$params['owner'].'" AND lastupdated = "'.$params['lastupdated'].'"';
                $this->db->query($sql);
                return $this->db->affected_rows();
        }
        
        public function updateStatisticBookOk($params){
                $sql = 'UPDATE '.$this->table_name.' SET `booking_ok` = `booking_ok` + 1 WHERE `owner` = "'.$params['owner'].'" AND lastupdated = "'.$params['lastupdated'].'"';
                $this->db->query($sql);
                return $this->db->affected_rows();
        }
        
        public function statiticUsers($beginTime=NULL,$endTime=NULL,$idshow=NULL){
            $this->db->select('usr.id,usr.username, sum(st.booking_hold) AS booking_hold,sum(st.booking_ok) AS booking_ok, st.lastupdated AS lastupdated');
            $this->db->from($this->table_name .' AS st');
            $this->db->join('users AS usr','usr.id = st.owner','inner');
            $this->db->where('usr.active',1);
            if($beginTime){
                $this->db->where('`st`.`lastupdated` >=',$beginTime);
            }
            if($endTime){
                $this->db->where('`st`.`lastupdated` <=',$endTime);
            }
//            if($beginTime == NULL AND $endTime = NULL){
//                $this->db->where('DATE(`st`.`lastupdated`) = CURDATE()');
//            }
            if($idshow){
                $this->db->where('usr.id > '.$idshow[0]);
                $this->db->where('usr.id < '.$idshow[1]);
            }
            $this->db->group_by('`st`.`owner`');
            $query = $this->db->get();

            return $query->result();
        }
}